package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("log:{}",shoppingCart.toString());
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        LambdaQueryWrapper<ShoppingCart> shoppingCartQuery = new LambdaQueryWrapper<>();
        shoppingCartQuery.eq(currentId != null,ShoppingCart::getUserId,currentId);

        //判断当前是菜品还是套餐
        if (shoppingCart.getDishId() != null){
            shoppingCartQuery.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            shoppingCartQuery.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //判断当前菜品或套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(shoppingCartQuery);

        if (cartServiceOne != null){
            //如果已经存在，则加1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //如果不存在，则添加到购物车，默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }

    /**
     * 减少购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //设置购物车用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //构建条件构造器
        LambdaQueryWrapper<ShoppingCart> cartQuery = new LambdaQueryWrapper<>();
        cartQuery.eq(ShoppingCart::getUserId,shoppingCart.getUserId());

        //判断是套餐还是菜品
        if (shoppingCart.getDishId() != null){
            //是菜品
            cartQuery.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //是套餐
            cartQuery.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //进行查询
        ShoppingCart cartServiceOne = shoppingCartService.getOne(cartQuery);
        Integer number = cartServiceOne.getNumber();
        if (number != null || number > 0) {
            cartServiceOne.setNumber(number-1);
            shoppingCartService.updateById(cartServiceOne);
            shoppingCart = cartServiceOne;
            //如果购物车减完之后为零，则应该清空购物车
            if(shoppingCart.getNumber() == 0){
                shoppingCartService.remove(cartQuery);
            }
            return R.success(shoppingCart);
        }else {
            return null;
        }
    }


    /**
     * 查询购物车数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //根据用户id查询购物车数据
        LambdaQueryWrapper<ShoppingCart> shoppingCartQuery = new LambdaQueryWrapper<>();
        shoppingCartQuery.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartQuery.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(shoppingCartQuery);
        return R.success(list);
    }



    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete(){
        //根据用户id删除购物车数据
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);

        return R.success("清空购物车成功");
    }

}
